package com.dc.base.utils

import android.content.Context
import android.content.Intent
import android.view.View

inline fun <reified T> T?.notNull(notNullAction: (T) -> Unit, nullAction: () -> Unit = {}) {
    if (this == null) {
        nullAction()
    } else {
        notNullAction(this)
    }
}

inline fun <reified T> startActivity(context: Context, block: Intent.() -> Unit) {
    val intent = Intent(context, T::class.java)
    intent.block()
    context.startActivity(intent)
}

/**
 * 功能防抖
 */
var lastClickTime = 0L
fun View.clickNoRepeat(interval: Long = 1000, action: (view: View) -> Unit) {
    setOnClickListener {
        var currentTime = System.currentTimeMillis()
        if (lastClickTime != 0L && (currentTime - lastClickTime) < interval) {
            return@setOnClickListener
        }
        lastClickTime = currentTime
        action(it)
    }
}